package ru.urfc.portal.ui.client.renderers;

import ru.urfc.portal.ui.client.descriptor.GridField;

import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.dom.client.Element;
import com.google.gwt.i18n.client.NumberFormat;

public class IntegerRenderer extends SimpleRenderer {

	private int divider;
	private NumberFormat format;
	private String ending;
	
	public IntegerRenderer(GridField field, Element element) {
		super(field, element);
		init(1, "###,###,###", "");
	}

	public IntegerRenderer setKWattsFormat() {
		init(1000, "###,###,##0.0", " КВт");
		return this;
	}	

	public IntegerRenderer setWattsFormat() {
		init(1, "###,###,##0", " Вт");
		return this;
	}	
	
	public IntegerRenderer setMHzFormat() {
		init(1000000, "###,###,##0.0000", " МГц");
		return this;
	}	

	public IntegerRenderer setMHzFormatForList() {
		init(1000000, "###,###,##0.0000", "");
		return this;
	}	

	public IntegerRenderer setDoubleFormat() {
		init(1, "###,###,##0.00", "");
		return this;
	}	
	
	public IntegerRenderer setMoneyFormat() {
		init(1, "###,###,##0.00", " р.");
		return this;
	}	
	
	public IntegerRenderer setMetersFormat() {
		init(1, "###,###,##0.##", " м");
		return this;
	}
	
	protected void init(int divider, String format, String ending) {
		this.divider = divider;
		this.format = NumberFormat.getFormat(format);
		this.ending = ending;
	}

	@Override
	public String render(ModelData modelData) {
		Long value = field.getValue(modelData);
		return render(value == null? null: value * 1D);
	}

	String render(Double value) {
		if (value == null) {
			return "";
		}
		return format.format(value / divider)/*.replace(",", " ").replace(".", ",")*/ + ending;
	}

}
